package com.company.sort;

import java.util.Random;

/**
 * @author shenhan
 * @create 2020/2/1 17:54
 * @description
 */
public class QuickSort {
    public static void main(String[] args) {
        int[] arr = new int[8000000];
        Random random = new Random();
        for (int i = 0; i < 8000000; i++) {
            arr[i] = random.nextInt();
        }
        final long l1 = System.currentTimeMillis();
        quickSort(arr, 0, arr.length - 1);
        final long l2 = System.currentTimeMillis();
        System.out.println(l2 - l1);
    }

    private static void quickSort(int[] arr, int left, int right) {
        int l = left;
        int r = right;
        int pivot = arr[(left + right) / 2];
        int temp;
        while (l < r) {
            while (arr[l] < pivot) {
                l += 1;
            }
            while (arr[r] > pivot) {
                r -= 1;
            }
            if (l >= r) {
                break;
            }
            temp = arr[l];
            arr[l] = arr[r];
            arr[r] = temp;
            if (arr[l] == pivot) {
                r -= 1;
            }
            if (arr[r] == pivot) {
                l += 1;
            }
        }
        if (l == r) {
            l += 1;
            r -= 1;
        }
        if (left < r) {
            quickSort(arr, left, r);
        }
        if (right > l) {
            quickSort(arr, l, right);
        }
    }
}
